package com.hudson.plugin.ccheck.service.impl;


import com.hudson.plugin.ccheck.domain.dao.CcheckresultDAOInterface;
import com.hudson.plugin.ccheck.internal.domain.entity.CcheckresultEntity;
import com.hudson.plugin.ccheck.service.CCheckDBStore;
import com.hudson.plugin.server.hibernateDB.domain.dao.*;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.BuildsEntity;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.HudsonsEntity;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.ProjectsEntity;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.ViewsEntity;
import com.hudson.plugin.server.hibernateDB.service.impl.ReportDBServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 4/26/11
 * Time: 9:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class CCheckDBStoreImpl extends ReportDBServiceImpl implements CCheckDBStore {
    private final static Logger logger = LoggerFactory.getLogger(CCheckDBStoreImpl.class);

    private HudsonsDAOInterface hudsonDAO;

    private ViewsDAOInterface viewsDAO;

    private ProjectsDAOInterface projectsDAO;

    private BuildsDAOInterface buildsDAO;

    private BuildRelationsDAOInterface buildRelationsDAO;

    private CcheckresultDAOInterface ccheckresultDAO;

    private Map<String, String> ccheckResultJson;

    public Map<String, String> storeToDB(Map<String, Map<String, String>> valuePairsContainer) throws Exception {
        logger.info("!!!!!!!!!!!!!!!!CCheckDBStore!!!!!!!!!!!!!!");
        Map<String, String> values = valuePairsContainer.get("CCheck");
        ccheckResultJson = values;
        super.storeToDB(valuePairsContainer);

        return null;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void updatePluginTable(Map<String, String> valuePairs, HudsonsEntity hudsonsEntity, ViewsEntity viewsEntity, ProjectsEntity projectsEntity, BuildsEntity buildsEntity) throws Exception {
        super.updatePluginTable(valuePairs, hudsonsEntity, viewsEntity, projectsEntity, buildsEntity);    //To change body of overridden methods use File | Settings | File Templates.
        CcheckresultEntity ccheckresult = new CcheckresultEntity();
        ccheckresult.setBuildid(buildsEntity.getId());
        ccheckresult.setDetails(ccheckResultJson.get("GetDetails"));
        ccheckresult.setDisplayname(ccheckResultJson.get("GetDisplayName"));
        //ccheckresult.setResultreason();
        ccheckresult.setScore(ccheckResultJson.get("GetScore"));
        ccheckresult.setSummary(ccheckResultJson.get("GetSummary"));
        List<Integer> ccheckIds = getCcheckresultDAO().findCCheckResultIdByBuildsId(buildsEntity.getId());
        if (ccheckIds.size() > 0) {
            ccheckresult.setId(ccheckIds.get(0));
            getCcheckresultDAO().merge(ccheckresult);
        } else {
            getCcheckresultDAO().merge(ccheckresult);
        }
    }


    public HudsonsDAOInterface getHudsonDAO() {
        return hudsonDAO;
    }

    public void setHudsonDAO(HudsonsDAOInterface hudsonDAO) {
        this.hudsonDAO = hudsonDAO;
    }

    public ViewsDAOInterface getViewsDAO() {
        return viewsDAO;
    }

    public void setViewsDAO(ViewsDAOInterface viewsDAO) {
        this.viewsDAO = viewsDAO;
    }

    public ProjectsDAOInterface getProjectsDAO() {
        return projectsDAO;
    }

    public void setProjectsDAO(ProjectsDAOInterface projectsDAO) {
        this.projectsDAO = projectsDAO;
    }

    public BuildsDAOInterface getBuildsDAO() {
        return buildsDAO;
    }

    public void setBuildsDAO(BuildsDAOInterface buildsDAO) {
        this.buildsDAO = buildsDAO;
    }

    public BuildRelationsDAOInterface getBuildRelationsDAO() {
        return buildRelationsDAO;
    }

    public void setBuildRelationsDAO(BuildRelationsDAOInterface buildRelationsDAO) {
        this.buildRelationsDAO = buildRelationsDAO;
    }


    public CcheckresultDAOInterface getCcheckresultDAO() {
        return ccheckresultDAO;
    }

    public void setCcheckresultDAO(CcheckresultDAOInterface ccheckresultDAO) {
        this.ccheckresultDAO = ccheckresultDAO;
    }
}
